package com.albd.intercept;

import java.io.File;
import java.util.LinkedHashMap;

import com.albd.utils.LogUtil;
import com.albd.utils.ShellCommand;
import com.albd.utils.VerifyUtil;

import agj.hooks.BBinderListener;
import android.R.string;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;

public class InterceptIAM implements InterceptWorker
{

	static final int START_ACTIVITY_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION + 2;
	static final int GET_CONTENT_PROVIDER_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION + 28;

	private static long lastTime = 0;
	private static InterceptIAM mInstance;
	// key: the new code; value: the old code
	@SuppressLint("UseSparseArrays") private LinkedHashMap<Integer, Integer> AmVCodeTable;
	static final String IN_TYPE="application/vnd.android.package-archive";

	public static InterceptIAM getInstance()
	{
		if(mInstance == null)
		{
			mInstance = new InterceptIAM();
		}
		return mInstance;
	}

	private InterceptIAM()
	{
		AmVCodeTable = new LinkedHashMap<Integer, Integer>();
	}

	private static final String tag = "VerifyIAM";	

	@Override public boolean onTransact(String aDes, int code, Parcel data, Parcel reply, int flags) throws RemoteException
	{
		//LogUtil.e(tag, "enter onTransact", LogUtil.VERIFY_IAM);
		boolean vRet = false;
		int vCallPid = Binder.getCallingPid();
		String vCallPro = ShellCommand.getProcessName(vCallPid);
		data.setDataPosition(0);
		data.enforceInterface(aDes);
		//LogUtil.d(tag, "the new code is " + code + " binder des is" + aDes+"processName=="+vCallPro,
				//LogUtil.VERIFY_IAM);
//		if("com.android.packageinstaller".equals(vCallPro)||"com.dianxinos.optimizer.channel".equals(vCallPro)||vCallPro.startsWith("com.wandoujia.phoenix2")){
//			code=3;
//		}
		
			switch (code) {

			case START_ACTIVITY_TRANSACTION:
			{
				
				Log.v("ks_test", "START_ACTIVITY_TRANSACTION vCallPro=="+vCallPro);
				@SuppressWarnings("unused") IBinder vTmp = data.readStrongBinder();
				if(Build.VERSION.SDK_INT >= 18){
					@SuppressWarnings("unused") String vCallPkgName = data.readString();
				}
				Intent intent = Intent.CREATOR.createFromParcel(data);
				if(intent != null){
					String type = intent.getType();
//					String action=intent.getAction();
//					String dataString=intent.getDataString();
//					String test=intent.getScheme();
//					Log.d("ks_test", "get_type  " + type+" acton=="+action+" dataString=="+dataString+" test=="+test+" package=="+intent.getPackage());
					if(type != null){
					Log.d("ks_test", "get_type is " + type);
					   if(type.equals(IN_TYPE)){
						   //TODO 判断安装器data/data/包名不存在，直接将vRet=false   installer包名  处理卸载后问题
						   File app = new File("data/data/com.android.packageinstalier");
						   LogUtil.d("ks_test", "data/data/com.android.packageinstalier existes : " + app.exists(), true);
						   if(!app.exists()){
							   vRet = false;
						   }else{
							   Uri vTmpUri = intent.getData();
								String vNum = null;
								if(vTmpUri != null){
									vNum = vTmpUri.toString();
									LogUtil.d("ks_test", "get_uri is " + vNum, true);
									vRet = VerifyUtil.queryAllowed(vCallPro, vCallPid, 11, vNum, null);
								}   
						   }
					   }
					}
						
				
				if(vRet){
					reply.writeNoException();
					reply.writeInt(0);
				}				
				break;
			}
			}
			default:
				if(System.currentTimeMillis() - lastTime > 1000){
					LogUtil.i("wmm", "default case ", true);
				}
				if(System.currentTimeMillis() - lastTime > 10 * 60 * 1000){
					LogUtil.i("wmm", "default case upClient called", true);
					lastTime= System.currentTimeMillis();
					VerifyUtil.upClient();
				}
				break;

			}
		
		return vRet;

	}

}
